package com.haozi.ehub.core.convert;

import com.haozi.ehub.core.util.CodeUtils;
import com.haozi.ehub.core.util.RSAEncoderUtils;
import com.haozi.ehub.entity.entities.AdminInfo;
import com.haozi.ehub.entity.entities.JwtUserInfo;
import com.haozi.ehub.entity.entities.UserInfo;

/**
 * 实体类转换类
 * @author ：terry
 * @date ：Created in 2020/7/3 10:56
 * @description 实体类转换
 * @version: 1.0
 */
public class JwtUserConvert {

    /**
     * 将AdminInfo或者UserInfo转换成JwtUserInfo
     * @param entity 传入AdminInfo或者UserInfo实体类
     * @author     ： terry
     * @date       ： Created in 2020/7/3 11:00
     */
    public static JwtUserInfo convert2JwtUser(Object entity){
        JwtUserInfo jwtUserInfo = new JwtUserInfo();
        if(entity instanceof AdminInfo){
            //获取AdminInfo对象
            AdminInfo adminInfo = (AdminInfo)entity;

            //将用户名加密
            final String encryptName = RSAEncoderUtils.encrypt(CodeUtils.ADMIN_PREFIX + CodeUtils.UNDERLINE + adminInfo.getAdminName());

            //封装JwtUserInfo对象
            jwtUserInfo.setEncryptName(encryptName);
            jwtUserInfo.setUserId(adminInfo.getAdminId());
            jwtUserInfo.setUserName(adminInfo.getAdminName());
            jwtUserInfo.setPassword(adminInfo.getAdminPassword());
            jwtUserInfo.setRoleList(adminInfo.getRoleList());

        }else if(entity instanceof UserInfo){
            //获取UserInfo对象
            UserInfo userInfo = (UserInfo)entity;

            //将用户名加密
            final String encryptName = RSAEncoderUtils.encrypt(CodeUtils.USER_PREFIX + CodeUtils.UNDERLINE + userInfo.getUserName());

            //封装JwtUserInfo对象
            jwtUserInfo.setEncryptName(encryptName);
            jwtUserInfo.setUserId(userInfo.getUserId());
            jwtUserInfo.setUserName(userInfo.getUserName());
            jwtUserInfo.setPassword(userInfo.getUserPassword());
        }
        return jwtUserInfo;
    }
}
